<?php $this->display(tpl('head')); ?>
<style>
	body{
		background-color: #eee;
	}
	.cloud .layui-flow-more {
		margin: 10px 0;
		text-align: center;
		color: #999;
		font-size: 14px;
		float: left;
		width: 100%;
	}
	.cloud_details{
		padding: 10px;
		display: none;
	}
</style>
<div class="cloud" id="cloud_list">
</div>

<div class="login_cloud" id="login_cloud" style="display: none;">
	<form class="layui-form">
		<table class="layui-table">
			<tr>
				<td>用户名:</td>
				<td>
					<input type="text" class="layui-input" name="cloud_user"/>
				</td>
			</tr>
			<tr>
				<td>密码：</td>
				<td>
					<input type="password"  class="layui-input" name="cloud_pass"/>
				</td>
			</tr>
			<tr>
				<td colspan="2" align="center">
					<input type="hidden" name="method" value="login" />
					<button lay-submit lay-filter="login_cloud" class="layui-btn layui-btn-normal">登陆</button>
					<a target="_blank" href="<?php echo Upgrade_URL ?>/login/index/reg/" class="layui-btn layui-btn-normal">注册</button>
				</td>
			</tr>
		</table>
	</form>
</div>
<div class="cloud_details" id="details"></div>
<div class="cloud_payment" id="payment">
	<form class="layui-form">
		<div>
			<span>名&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;称:</span>
			<span class="b" id="payment_title"></span>
		</div>
		<div>
			<span>授权版本:</span>
			<span class="b" id="payment_authorized_version"></span>
		</div>
		<div>
			<span>需要支付:</span>
			<span class="red b" id="payment_needrmb"></span>
		</div>
		<div>
			<span>所剩余额:</span>
			<span class="red b" id="rmb"></span>
		</div>
		<div class="payment_list">
			<span id="payment_list"></span>
			<botton class="layui-btn layui-btn-danger" lay-submit lay-filter="payment_button">余额支付</botton>
		</div>
	</form>
</div>
<style>
	.cloud_tpl{
	
	}
</style>
<div class="cloud_tpl" id="cloud_tpl">
	<form class="layui-form">
		<div>
			<span>请选择安装位置:</span>
			<span class="b" id="tpl_selected_location">input</span>
		</div>
		<div class="payment_list">
			<input type="hidden" id="is_modular" name="is_modular" value="1" />
			<botton class="layui-btn layui-btn-danger" lay-submit lay-filter="tpl_button">确定安装</botton>
		</div>
	</form>
</div>
<script>
	var data_json=next=Object,rmb=Number,page=1,all_page=0,method='<?php echo $method ?>';
	var payment_data=Object;
	var install_data=<?php echo $data ?>;
	var is_tpl_selected=false;
	var version_initial_name='<?php echo C('Version_initial_name') ?>';
	layui.use(['form','flow'], function(){
		var form=layui.form;
		flow = layui.flow;
		//价格选择器
		form.on('radio(price)', function(data){
			var i=data.elem.attributes['key'].value;
			var value=data.value;
			var data=data_json[i];
			var price=data.price_list[value].price;
			var obj=$('#price_'+i);
			if(price > 0){
				obj.html('￥ '+price+' 元');
			}else{
				obj.html('免费');
			}
			return false;
		});
		form.on('submit(payment_button)', function(data){
			layer.prompt({title: '请输入支付密码',formType: 1},function(value, index, elem){
				payment_data.rmb_pass=value;
				payment_data.method="pay_rmb";
				$.ajax({
					url:"/admin/cloud/get_cloud_data",
					type: 'POST',
					data:payment_data,
					dataType: 'json',
					success: function(res){
						if(res.code==0){
							layer.closeAll();
							$('#install_'+payment_data.i+ ' button:first').html('已下载');
							$('#install_'+payment_data.i+ ' button:first').removeClass('layui-btn-normal');
							$('#install_'+payment_data.i+ ' button:first').addClass('layui-btn-disabled');
							$('#install_'+payment_data.i+ ' button:first').removeAttr('lay-submit');
							$('#install_'+payment_data.i+ ' button:first').removeAttr('lay-filter');
							if(method=='modular' || method=='plugin'){
								layer.msg('数据已下载完毕，请至模块管理，或者插件管理进行手动安装');
							}else if(method=='hook'){
								layer.msg('钩子已经下载完毕，并成功导入');
							}else if(method=='tpl'){
								layer.msg('数据已下载完毕，请至网站设置->模板目录选择模板，设置后请更新全站缓存');
							}
						}else{
							layer.msg(res.text);
						}
					},
				})
				layer.close(index);
			});
			return false;
		});
		
		//安装选择的模板
		form.on('submit(tpl_button)', function(data){
			var arr=[];
			$("input:checkbox[name=tpl_dir]:checked").each(function(){
				arr.push($(this).val());
			});
			if(arr.length<1){
				layer.msg('请选择一个要安装到的模块,或者插件');
				return false;
			}
			data.field.tpl_dir=arr;
			data.field=$.extend(payment_data,data.field);
			get_install_data(data.field);
			return false;
		})
		
		//安装
		form.on('submit(install)', function(data){
			if(method=='modular' || method=='tpl'){
				//判断是否需要预安装其它模块
				var test=data_json[data.field.i];
				var tpl_ = true;
				if(method=='tpl' && test.like_modular && test.like_modular[0].initial_name=='mvchome'){
					if(version_initial_name != 'mvchome'){
						layer.msg('你的版本不是mvchome版本，不能安装此模板');
						return false;
					}
					tpl_= false;
					data.field.is_modular=1;
					data.field.tpl_dir=['mvchome'];
				}
				if(test.like_modular.length>0 && tpl_==true){
					var is_install_modular=true;
					var html='安装'+test.title+'需预安装模块';
					for(var i=0;i<test.like_modular.length;i++){
						var like_modular=test.like_modular[i];
						if($.inArray(like_modular.initial_name,install_data.modular)=='-1'){
							is_install_modular=false;
						}
						html +=like_modular.title+',';
					}
					if(is_install_modular==false){
						layer.msg(html);
						return false;
					}
				}

				if(method=='tpl' && test.like_plugin ){
					var like_plugin =test.like_plugin[0].initial_name;
					var dir=install_data.plugin_tpl[like_plugin][0].dir;
					data.field.is_modular=0;
					data.field.tpl_dir=[dir];
				}
				
				//判断是否需要预安装其它插件
				if(test.like_plugin.length>0){
					var is_install_plugin=true;
					var html='安装'+test.title+'需预安装插件<p>';
					for(var i=0;i<test.like_plugin.length;i++){
						var like_plugin=test.like_plugin[i];
						if($.inArray(like_plugin.initial_name,install_data.plugin)=='-1'){
							is_install_plugin=false;
						}
						html +=like_plugin.title+',';
					}
					html+='</p>';
					if(is_install_plugin==false){
						layer.msg(html);
						return false;
					}
				}
			}
			data.field.method="install";
			data.field.from=method;
			if(method=='tpl' && tpl_==true){
				//判断是不是安装了多分
				var tpl_data=data_json[data.field.i];
				if(tpl_data.like_modular){
					var t_initial_name=tpl_data.like_modular[0].initial_name;
						payment_data=data.field;
						$('#tpl_selected_location').html('');
						for (var i = 0; i < install_data.modular_tpl[t_initial_name].length; i++) {
							var selected=install_data.modular_tpl[t_initial_name][i];
							var html='<input type="checkbox" name="tpl_dir" value="'+selected.dir+'"  title="'+selected.name+'" />';
							$('#tpl_selected_location').append(html);
						}
						
				}else if(tpl_data.like_plugin){
					$('#is_modular').val('0');
					var t_initial_name=tpl_data.like_plugin[0].initial_name;
						payment_data=data.field;
						$('#tpl_selected_location').html('');
						console.log(install_data);
						for (var i = 0; i < install_data.plugin_tpl[t_initial_name].length; i++) {
							var selected=install_data.plugin_tpl[t_initial_name][i];
							var html='<input type="checkbox" name="tpl_dir" value="'+selected.dir+'"  title="'+selected.name+'" />';
							$('#tpl_selected_location').append(html);
						}
				}
				form.render();
				//调起选择器
				layer.open({
					title:'请选择要将该模板安装到的位置',
					area:['30%'],
					type: 1,
					content: $('#cloud_tpl')
				});
				return false;
			}
			get_install_data(data.field);
			return false;
		})
		
		//获取安装数据
		function get_install_data(data){
			$.ajax({
				url:"/admin/cloud/get_cloud_data",
				type: 'POST',
				data:data,
				dataType: 'json',
				success: function(res){
					if(res.code==1){//调起登陆模式
						login_cloud();
						layer.msg(res.text);
					}else if(res.code==100){//调起支付模式
						payment_data=data;
						if(res.payment.length>0){
							$('#payment_list').html('');
							for (var i = 0; i < res.payment.length; i++) {
								var html='<a class="layui-btn layui-btn-normal" href="'+res.payment[i].url+'" target="_blank">'+res.payment[i].name+'</a>';
								$('#payment_list').prepend(html);
							}
							var data_one=data_json[data.i];
							$('#payment_title').html(data_one.title);
							for (var i = 0; i < data_one.price_list.length; i++) {
								if(i==data.price){
									$('#payment_authorized_version').html(data_one.price_list[i].name);
									$('#payment_needrmb').html(data_one.price_list[i].price+' 元');
								}
							}
							$('#rmb').html(rmb+' 元');
							layer.open({
								title:'支付',
								area:['30%'],
								type: 1,
								content: $('#payment')
							});
						}
						layer.msg(res.text);
					}else{
						layer.closeAll();
						$('#install_'+data.i+ ' button:first').html('已下载');
						$('#install_'+data.i+ ' button:first').removeClass('layui-btn-normal');
						$('#install_'+data.i+ ' button:first').addClass('layui-btn-disabled');
						$('#install_'+data.i+ ' button:first').removeAttr('lay-submit');
						$('#install_'+data.i+ ' button:first').removeAttr('lay-filter');
						if(method=='modular' || method=='plugin'){
							layer.msg('数据已下载完毕，请至模块管理，或者插件管理进行手动安装');
						}else if(method=='hook'){
							layer.msg('钩子已经下载完毕，并成功导入');
						}else if(method=='tpl'){
							layer.msg('数据已下载完毕，请至网站设置->模板目录选择模板，设置后请更新全站缓存');
						}
					}
				},error:function (XMLHttpRequest, textStatus, errorThrown) {
					layer.msg(textStatus+errorThrown);
				}
			})
		}
		
		
		//查看介绍
		form.on('submit(desc)', function(data){
			var i=data.elem.attributes['data-key'].value;
			var data=data_json[i];
			var obj= $('#details');
				obj.html(data.content);
				layer.open({
					title:'详情介绍',
					shadeClose:true,
					area:'50%',
					type: 1,
					content: obj
				});
			return false;
		})
		//登陆云平平台
		form.on('submit(login_cloud)', function(data){
			data.field.json=true;
			data.field.method='login';
			$.ajax({
				url:"/admin/cloud/get_cloud_data",
				type: 'POST',
				data:data.field,
				dataType: 'json',
				success: function(res){
					if(res.code==0){
						$('#cloud_list').html('');
						layer.closeAll();
						flow.load({
							elem: '#cloud_list',
							isLazyimg:true,
							done: function(_page, _next){
								get_cloud_data();
								page=_page;
								next=_next;
							}
						});
					}
					layer.msg(res.text);
				}
			});
			return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
		});
		//弹出云平台登陆窗口
		function login_cloud(){
			layer.open({
				title:'登陆云平台',
				area:['30%'],
				type: 1,
				content: $('#login_cloud')
			});
		}
		//瀑布流
		flow.load({
			elem: '#cloud_list',
			isLazyimg:true,
			done: function(_page, _next){
				get_cloud_data();
				page=_page;
				next=_next;
			}
		});
		//去云平台获取数据
		function get_cloud_data(){
			$.ajax({
				url:"/admin/cloud/get_cloud_data",
				type: 'POST',
				data:{'json':true,'page':page,'method':method},
				dataType: 'json',
				success: function(res){
					if(res.code==0){
						if(page==1){
							all_page=res.page;
						}
						if(res.data){
							data_json=res.data;
							fordata();
							form.render();
						}
						rmb=res.rmb;
					}else{
						login_cloud();
					}
				}
			});
		}
		//循环输出数据
		function fordata(){
			var lis = [];
			for (var i = 0; i < data_json.length; i++) {
				var data=data_json[i];
				
				var html='	<form class="layui-form">';
					html+='		<div class="list">';
					html+='			<div class="thumb">';
					html+='				<a target="_blank" href="'+data.thumb+'"><img lay-src="'+data.thumb+'" ></a>';
					html+='			</div>';
					html+='			<div class="title">';
					html+='				<a target="_blank" href="'+data.url+'" id="title_'+i+'">'+data.title+'</a>';
					html+='			</div>';
					html+='			<div class="ot">';
					html+='				<span>版&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;本：</span>';
					html+='				<span class="red b">'+data.version+'</span>';
					html+='			</div>';
					html+='			<div class="ot">';
					html+='				<span>开&nbsp;&nbsp;发&nbsp;&nbsp;者：</span>';
					html+='				<span><a target="_blank" href="'+data.homepage+'">'+data.username+'</a></span>';
					html+='			</div>';
					if(data.price_list[0].price>0){
					html+='			<div class="price" id="price_'+i+'">￥ '+data.price_list[0].price+' 元</div>';
					}else{
					html+='			<div class="price" id="price_'+i+'">免费</div>';
					}
					html+='			<div class="ot">';
					html+='				<span>简&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;介：</span>';
					html+='				<span title="<?php echo $row[description] ?>">'+data.description+'</span>';
					html+='			</div>';
					html+='			<div class="ot">';
					html+='				<span>安装次数：</span>';
					html+='				<span>'+data.install_num+' 次</span>';
					html+='			</div>';
					html+='			<div class="ot">';
					html+='				<span>更新时间：</span>';
					html+='				<span>'+data.update+'</span>';
					html+='			</div>';
					html+='			<div class="price_list_sort">';
					html+='				<span>售&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;价</span>';
					html+='			</div>';
					html+='			<div class="ot">';
					html+='				<div class="price_list">';
											for (var ii = 0; ii < data.price_list.length; ii++) {
												var checked='';
												if(ii==0){
													checked=" checked";
												}
					html+='						<input '+checked+' key="'+i+'" lay-filter="price" type="radio" name="price" value="'+ii+'" title="'+data.price_list[ii].name+'">';
											}
					html+='				</div>';
					html+='			</div>';
					html+='			<div class="install" id="install_'+i+'">';
					html+='				<input type="hidden" name="i" value="'+i+'">';
					html+='				<input type="hidden" name="aid" value="'+data.aid+'">';
					if((method=='tpl' && $.inArray(data.aid,install_data.install_cloud_tpl) != '-1') ||(method=='hook' && $.inArray(data.aid,install_data.hook) != '-1') || (method=='modular' && $.inArray(data.initial_name,install_data.modular)!= '-1') || (method=='plugin' && $.inArray(data.initial_name,install_data.plugin)!= '-1')){
						html+='				<button type="button" class="layui-btn layui-btn-disabled" >已安装</button>';
					}else{
						html+='				<button type="button" class="layui-btn layui-btn-normal" lay-submit lay-filter="install">在线安装</button>';
					}
					
					html+='				<button type="button" class="layui-btn layui-btn-danger" data-key="'+i+'" lay-submit lay-filter="desc">说明文档</button>';
					html+='			</div>';
					html+='		</div>';
					html+='	</form>';
				lis.push(html);
			}
			console.log(next);
			next(lis.join(''), page < all_page);
		}
		
	});
</script>
<?php $this->display(tpl('foot')); ?>
